<?php

class Product extends Controller {

	function Product()
	{
		parent::Controller();
		$this->load->helper('admin');
		
		construct_admin_controller($this->EzAuth_Model);
		
		
	}
	
	
	//do something before we load the page:
	//check to see if you are logged in
	
	function _remap($method) {
		checkauth($this, $method, $this->EzAuth_Model);
	}
	

	
	function index()
	{
		
	}
	
	function edit($message = '') {
		$this->load->helper('form');
		$data = get_page_ready('addproduct', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		$data['location'] = 'addproduct';
		$data['message'] = $message;
		
		//get the product
		$id = $this->uri->segment(4);
		$data['edit'] = $this->Products_Model->get_item($id);
		
		
		$this->load->view('admin/addproduct', $data);
	}
	
	function add($message = '') {
		$this->load->helper('form');
		$data = get_page_ready('addproduct', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		$data['location'] = "addproduct";
		$data['message'] = $message;
		$this->load->view('admin/addproduct', $data);
	}
	
	function processadd() {
		
		/*
		 $_POST array(9) {
			["catname"]=> string(0) ""
			["category"]=> array(2) {
				[166]=> string(3) "166"
				[140]=> string(3) "140"
			}
			["title"]=> string(14) "My New Product"
			["upc"]=> string(4) "1234"
			["sku"]=> string(4) "5678"
			["price"]=> string(5) "59.99"
			["description"]=> string(24) "This is a great product."
			["invisible"] => "false"
			["MAX_FILE_SIZE"]=> string(5) "50000"
			["images"]=> array(2) {
				[0]=> string(101) "http://gavin-blairs-computer.local/~gavinblair/Busby%20Google%20Code/uploads/Commandments_Poster3.jpg"
				[1]=> string(88) "http://gavin-blairs-computer.local/~gavinblair/Busby%20Google%20Code/uploads/zoom-14.gif"
			}
		}
		*/
		
		$newproduct['title'] = $_POST['title'];
		$newproduct['description'] = $_POST['description'];
		$newproduct['image'] = '';
		if(isset($_POST['images'])) { foreach ( $_POST['images'] as $img ) {
			//add the locations of images, separated by a comma
			$newproduct['image'] .= $img . ",";
		}}
		$newproduct['price'] = $_POST['price'];
		$newproduct['date'] = date("Y-m-d");
		$newproduct['invisible'] = $_POST['invisible'];
		$newproduct['upc'] = $_POST['upc'];
		$newproduct['sku'] = $_POST['sku'];
		
		$this->Products_Model->add($newproduct, $_POST['category']);
		
		$this->add('Product Successfully Added');
		
	}
	
	function ajax_upload() {
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '0';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload('userfile'))
		{
			echo $this->upload->display_errors('', '');
			
		}	
		else
		{
			//add the url to the database
			
			$data = $this->upload->data();
			echo base_url() . 'uploads/' . $data['file_name'];
			
		}
	}
	

}
?>